package com.sxx.beautifulcity.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.adapter.AroundAdapter;
import com.sxx.beautifulcity.database.AroundHelper;
import com.sxx.beautifulcity.dialog.CommonProgressDialog;
import com.sxx.beautifulcity.entity.DeliciousFoodData;
import com.sxx.beautifulcity.entity.PetData;
import com.sxx.beautifulcity.entity.SpecialityData;
import com.sxx.beautifulcity.entity.Tuan_list_Tongyong;
import com.sxx.beautifulcity.entity.greendao.AgritourismDaoEntity;
import com.sxx.beautifulcity.entity.greendao.AroundDaoEntity;
import com.sxx.beautifulcity.entity.greendao.HolidayDaoEntity;
import com.sxx.beautifulcity.entity.greendao.PluckDaoEntity;
import com.sxx.beautifulcity.entity.greendao.VillageDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;
import com.sxx.beautifulcity.constant.Constant;
import com.sxx.beautifulcity.utils.NetWorkUtils;
import com.sxx.beautifulcity.utils.VolleySingleton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 15/10/26.
 * 景区周边
 */
public class AroundActivity extends DialogBaseActivity
        implements OnMyItemClickListener, View.OnClickListener {
    //定义一个TAG作为将请求添加到请求 队列中的唯一标记
    private final static String TAG = "AroundActivity";
    //定义RecyclerView
    private RecyclerView line_recyclerView;
    //定义适配器
    private AroundAdapter adapter;
    private TextView titleTv , moreTv;
    private ImageView backIv;
    //定义数据库帮助类
    private AroundHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_around);
        initView();

        helper = new AroundHelper();

        List<AroundDaoEntity> entities = helper.queryAll();
        boolean network = NetWorkUtils.checkNet(this);
        /**判断是否有网,如果有网的话从网上拉去数据,如果没有网的话从数据库取数据*/
        if (network == true) {
            requestLineContent();
        }else{
            adapter.addData(entities);
        }


    }

    /***
     * 初始化控件
     */
    private void initView() {
        titleTv = (TextView) findViewById(R.id.titleTv);
        titleTv.setText("景区周边");
        backIv = (ImageView) findViewById(R.id.backIV);
        backIv.setOnClickListener(this);
        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");

        line_recyclerView = (RecyclerView) findViewById(R.id.line_recyclerView);

        adapter = new AroundAdapter(getSupportFragmentManager());
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        line_recyclerView.setLayoutManager(manager);
        line_recyclerView.setAdapter(adapter);
        adapter.setOnMyItemClickListener(this);
    }

    /***
     * 请求网络数据
     */
    private void requestLineContent() {
        //自定义Dialog
        final Dialog dialog = CommonProgressDialog.createLoadingDialog(this , "Loding.......");
        dialog.show();
        //通过常量类加载网址
        String path = Constant.LINE_URL;
        JsonObjectRequest request = new JsonObjectRequest(path, null,
                new Response.Listener<JSONObject>() {
                    Gson gson = new Gson();
                    List<AroundDaoEntity> datas = new ArrayList<>();

                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("list_data")) {
                            try {
                                JSONArray list_data = response.getJSONArray("list_data");
                                for (int i = 0; i < list_data.length(); i++) {
                                    String str = list_data.getString(i);
                                    AroundDaoEntity data = gson.fromJson(str, AroundDaoEntity.class);
                                    datas.add(data);
                                }
                                adapter.addData(datas);
                                helper.deleteAroundList(datas);
                                helper.saveAroundList(datas);
                                dialog.dismiss();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });
        VolleySingleton.getInstance().addRequest(request, TAG);
    }

    @Override
    public void onMyItemClick(Tuan_list_Tongyong data) {

    }

    @Override
    public void onMyItemClick(AroundDaoEntity data) {
        Intent intent = new Intent(this , AroundDetailActivity.class);
        intent.putExtra("linksid" , data.getLinksid());
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.left_out, R.anim.right_into);
    }

    @Override
    public void onMyItemClick(HolidayDaoEntity data) {

    }

    @Override
    public void onMyItemClick(AgritourismDaoEntity data) {

    }

    @Override
    public void onMyItemClick(PluckDaoEntity data) {

    }

    @Override
    public void onMyItemClick(VillageDaoEntity data) {

    }

    @Override
    public void onMyItemClick(SpecialityData data) {

    }

    @Override
    public void onMyItemClick(DeliciousFoodData data) {

    }

    @Override
    public void onMyItemClick(PetData data) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this , MainActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.left_out, R.anim.right_into);
    }
}
